import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    static final String FILEPATH = "D:\\测试临时文件\\新增高管\\src\\新增高管.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {
        Workbook workbook = WorkbookFactory.create(new File(FILEPATH));
        Sheet sheet = workbook.getSheetAt(1);

        int rowSize = sheet.getPhysicalNumberOfRows();
        for (int i = 2; i < rowSize; i++) {
            String Resume = sheet.getRow(i).getCell(17)+"";

            String[] strings = Resume.split("(\\.|,|，|。|;|；)");

            List<String> resList = new ArrayList<>();
            List<String> resList2 = new ArrayList<>();

           for(int index = 0; index < strings.length; index++){
               String str = strings[index];

               /**
                * 删除所有空格
                */
               str = str.replaceAll(" ", "");

               /**
                * 含有关键字“退休” 直接写入“退休”
                */
                if (str.contains("退休")){
                    resList.clear();
                    resList.add("退休");
                    break;
                }

                if ((str.contains("现任") || str.contains("现担任") || str.contains("至今") || str.contains("现为") ||
                     str.contains("迄今") || str.contains("目前担任") || str.contains("目前任") || str.contains("现就职") ) &&
                    (!str.contains("独立董事") && !str.endsWith("现任") && !str.endsWith("现担任") && !str.endsWith("至今") && !str.endsWith("现为") &&
                     !str.endsWith("迄今") && !str.endsWith("目前担任") && !str.endsWith("目前任") && !str.endsWith("现就职"))){
                    System.out.println(str);
                    resList.add(str);
                }
           }

           if (resList.size() == 0)
               for (int index = 0; index < strings.length; index++){
                   String str = strings[index];

                   if (str.endsWith("至今") && !str.contains("独立董事") && index+1 < strings.length){
                       resList2.add(str + "," + strings[++index]);
                   }

               }

           List<String> final_list = new ArrayList<>();
           if (resList.size() > 0)
               final_list.addAll(resList);
           else sheet.getRow(i);

           if (resList2.size() > 0)
               final_list.addAll(resList2);
           else sheet.getRow(i);

           StringBuilder sb = new StringBuilder();
           final_list.forEach(str -> {
               if (!str.equals(" "))
                   sb.append(str+";");
           });

           String final_str = sb.toString().contains("退休") ? "退休" : sb.toString();

           sheet.getRow(i).createCell(22).setCellValue(final_str);
        }
        workbook.write(new FileOutputStream("D:\\测试临时文件\\新增高管\\关键字提取\\新增高管.xlsx"));


    }

}
